package com.phpxiu.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.view.BaseShare;
import com.phpxiu.app.view.RoomVideoRoom;

/**
 * 直播结果显示
 * Created by 吴金洲 on 2016/9/4
 */
public class LiveResultDialog extends Dialog implements View.OnClickListener {
    private RoomVideoRoom videoRoom;
    private TextView watchCountView;
    private View hostView;
    private TextView ticketsView;
    private TextView praiseView;
    private TextView durationView;
    private boolean mInAVRoom=false;//是否已经走完进入房间流程，在房间中
    private View shareBar;//分享栏
    private ImageButton wbBtn,wxBtn,qqBtn,zoneBtn,friendBtn;
    private ImageButton fbBtn,ttBtn,insBtn,googleBtn;
    public LiveResultDialog(Context context) {
        super(context, R.style.slideRightDialog);
        if(context instanceof RoomVideoRoom){
            videoRoom=(RoomVideoRoom)context;
        }
        init(context);
    }

    /**
     * 初始化
     */
    final private void init(Context context){
        setContentView(R.layout.live_result_dialog);
        shareBar=findViewById(R.id.share_menus_bar);
        watchCountView=(TextView)findViewById(R.id.watch_count_view);
        hostView=findViewById(R.id.host_extra_result_box);
        ticketsView=(TextView)findViewById(R.id.tickets_count_view);
        praiseView=(TextView)findViewById(R.id.praise_count_view);
        durationView=(TextView)findViewById(R.id.duration_view);
        findViewById(R.id.live_result_close_btn).setOnClickListener(this);
        wxBtn=(ImageButton)findViewById(R.id.share_to_wx);
        wxBtn.setOnClickListener(this);
        friendBtn=(ImageButton)findViewById(R.id.share_to_friend);
        friendBtn.setOnClickListener(this);
        qqBtn=(ImageButton)findViewById(R.id.share_to_qq);
        qqBtn.setOnClickListener(this);
        zoneBtn=(ImageButton)findViewById(R.id.share_to_qq_zone);
        zoneBtn.setOnClickListener(this);
        wbBtn=(ImageButton)findViewById(R.id.share_to_sina);
        wbBtn.setOnClickListener(this);

        fbBtn=(ImageButton)findViewById(R.id.share_to_fb);
        fbBtn.setOnClickListener(this);
        ttBtn=(ImageButton)findViewById(R.id.share_to_tt);
        ttBtn.setOnClickListener(this);
        insBtn=(ImageButton)findViewById(R.id.share_to_ins);
        insBtn.setOnClickListener(this);
        googleBtn=(ImageButton)findViewById(R.id.share_to_google);
        googleBtn.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //返回键盘事件
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(videoRoom!=null){
                videoRoom.onLiveResultClose(mInAVRoom);
                dismiss();
            }
            return false;
        }else{
            return super.onKeyDown(keyCode,event);
        }
    }

    @Override
    public void onClick(View v) {
        if(videoRoom!=null){
            if(v.getId()==R.id.live_result_close_btn){
                videoRoom.onLiveResultClose(mInAVRoom);
                dismiss();
                return;
            }
            shareView(v.getId());
        }
    }

    /**
     * 隐藏分享栏
     */
    public void hideShareBar(){
        if(shareBar.getVisibility()==View.VISIBLE){
            shareBar.setVisibility(View.INVISIBLE);
        }
    }

    private void shareView(int id){
        if(videoRoom==null||videoRoom.roomShareContent==null){
            return;
        }
        wxBtn.setImageResource(R.mipmap.live_result_wx_icon_normal);
        qqBtn.setImageResource(R.mipmap.live_result_qq_icon_normal);
        zoneBtn.setImageResource(R.mipmap.live_result_zone_icon_normal);
        friendBtn.setImageResource(R.mipmap.live_result_friend_icon_normal);
        wbBtn.setImageResource(R.mipmap.live_result_wb_icon_normal);

        fbBtn.setImageResource(R.mipmap.live_result_fb_icon_normal);
        ttBtn.setImageResource(R.mipmap.live_result_tt_icon_normal);
        insBtn.setImageResource(R.mipmap.live_result_ins_icon_normal);
        googleBtn.setImageResource(R.mipmap.live_result_google_icon_normal);
        switch (id){
            case R.id.share_to_wx:
                wxBtn.setImageResource(R.mipmap.live_result_wx_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_WX,videoRoom.roomShareContent);
                break;
            case R.id.share_to_friend:
                friendBtn.setImageResource(R.mipmap.live_result_friend_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_WF,videoRoom.roomShareContent);
                break;
            case R.id.share_to_qq:
                qqBtn.setImageResource(R.mipmap.live_result_qq_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_QQ,videoRoom.roomShareContent);
                break;
            case R.id.share_to_qq_zone:
                zoneBtn.setImageResource(R.mipmap.live_result_zone_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_QZ,videoRoom.roomShareContent);
                break;
            case R.id.share_to_sina:
                wbBtn.setImageResource(R.mipmap.live_result_wb_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_WB,videoRoom.roomShareContent);
                break;
            case R.id.share_to_fb:
                fbBtn.setImageResource(R.mipmap.live_result_fb_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_FB,videoRoom.roomShareContent);
                break;
            case R.id.share_to_tt:
                ttBtn.setImageResource(R.mipmap.live_result_tt_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_TT,videoRoom.roomShareContent);
                break;
            case R.id.share_to_ins:
                insBtn.setImageResource(R.mipmap.live_result_ins_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_INS,videoRoom.roomShareContent);
                break;
            case R.id.share_to_google:
                googleBtn.setImageResource(R.mipmap.live_result_google_icon_selected);
                videoRoom.onRequestShareContentTo(BaseShare.PLATFORM_GG,videoRoom.roomShareContent);
                break;
        }
    }

    /**
     * 是显示主播结果
     * @param isHost 是否为主播
     */
    public void showHost(boolean isHost){
        if(isHost){
            if(hostView.getVisibility()!=View.VISIBLE){
                hostView.setVisibility(View.VISIBLE);
            }
        }else{
            if(hostView.getVisibility()==View.VISIBLE){
                hostView.setVisibility(View.GONE);
            }
        }
    }

    public void setWatchCountView(String count){
        if(watchCountView!=null){
            watchCountView.setText(count);
        }
    }

    public void setTicketsView(String income){
        if(ticketsView!=null){
            ticketsView.setText(income);
        }
    }

    public void setPraiseView(String praise){
        if(praiseView!=null){
            praiseView.setText(praise);
        }
    }

    /**
     * 设置直播时间长度
     * @param duration
     */
    public void setDurationView(String duration){
        if(durationView!=null){
            durationView.setText(duration);
        }
    }

    public void setInAVRoom(boolean inAVRoom){
        mInAVRoom=inAVRoom;
    }

    @Override
    public void dismiss() {
        videoRoom=null;
        super.dismiss();
    }
}
